package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.Account;
import com.example.chatchatbackend.entity.dto.Customer;
import com.example.chatchatbackend.entity.vo.request.RegisterVO;
import com.example.chatchatbackend.entity.vo.response.FriendsVO;
import com.example.chatchatbackend.mapper.AccountMapper;
import com.example.chatchatbackend.mapper.CustomerMapper;
import com.example.chatchatbackend.service.AccountService;
import com.example.chatchatbackend.service.CustomerService;
import com.example.chatchatbackend.service.MessageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Resource
    PasswordEncoder encoder;
    @Resource
    MessageService messageService;
    @Resource
    CustomerService customerService;

    @Resource
    CustomerMapper customerMapper;
    @Resource
    AccountMapper accountMapper;




    /**
     * 实现UserDetailsService,实现用户登录
     * @param username 账户名
     * @return UserDetails
     * @throws UsernameNotFoundException 用户未找到异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Account account = findAccountByUsername(username);
        if(account == null)
            throw new UsernameNotFoundException("用户名或密码错误");
        return User
                .withUsername(username)
                .password(account.getPassword())
                .roles(account.getRole())
                .build();
    }

    /**
     * 根据用户名查询对应账户
     * @param username 账户名
     * @return Account账户
     */
    @Override
    public Account findAccountByUsername(String username) {
        Account account = this.query()
                .eq("username", username)
                .one();
        log.info("{}",account);
        return account;

    }

    /**
     * 账户注册
     * @param registerVO 注册实体类vo
     * @return success null
     */
    @Override
    public String registerAccount(RegisterVO registerVO) {
        String password = encoder.encode(registerVO.getPassword());
        Account account = new Account(null, registerVO.getUsername(), password, registerVO.getEmail(),registerVO.getPhone(),
                "user", null, new Date(),1,registerVO.getAddress() );
        Customer customer = new Customer();
        BeanUtils.copyProperties(account, customer);
        customerMapper.insert(customer);
        if(this.save(account))
            return null;
        return "内部错误，请联系管理员";
    }

    /**
     * 根据用户id查询好友列表
     * @param id 用户id
     * @return 好友列表
     */
    @Override
    public List<FriendsVO> findFriendsById(int id) {
        List<Account> accounts = this.query()
                .ne("id", id)
                .list();
        List<FriendsVO> friendsVOS = new ArrayList<>();
        for (Account account : accounts) {
            String message = messageService.getLastMessageById(id, account.getId());
            friendsVOS.add(new FriendsVO(account.getId(), account.getUsername(), account.getAvatar(), message));
        }
        return friendsVOS;
    }



    @Override
    public List<Customer> syncCustomer() {
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Account::getRole, "user");
        List<Account> accountList = accountMapper.selectList(wrapper);
        log.info("{}", accountList);

        List<Customer> newCustomers = new ArrayList<>();
        for (Account account : accountList) {

            Customer existingCustomer = customerService.findAccountByUsername(account.getUsername());

            if (existingCustomer == null) {
                Customer newCustomer = new Customer(null, account.getUsername(), account.getEmail(), account.getPhone(), account.getRole(), account.getAvatar(), account.getRegisterTime(), account.getUsertagsId(), account.getAddress());
                log.info("{}", newCustomer);
                customerMapper.insert(newCustomer);
                newCustomers.add(newCustomer);
            } else {
                log.info("Customer with userName {} already exists. Skipping insert.", account.getUsername());
            }
        }

        return newCustomers;
    }



}
